package com.swfee.qinben.session;

import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.app.Application;
import android.util.Log;

import com.swfee.qinben.util.CurenActivitUtils;

public class MyApplication extends Application {

	private List<Activity> activityList = new LinkedList<Activity>();
	private static MyApplication instance;

	private MyApplication() {
	}

	// 单例模式中获取唯一的MyApplication实例
	public static MyApplication getInstance() {
		if (null == instance) {
			instance = new MyApplication();
		}
		return instance;

	}

	// 添加Activity到容器中
	public void addActivity(Activity activity) {
		activityList.add(activity);
	}

	// 遍历所有Activity并finish

	public void closeAllActivities() {
		for (Activity activity : activityList) {
			if(activity!=null ){
				activity.finish();
			}
			
		}
	}

	public void closeBefore() {
		Activity act = CurenActivitUtils.getCurActivity();

		if (activityList != null && activityList.size() > 2) {
			for (int i = 0; i < activityList.size(); i++) {
				Activity activity = activityList.get(i);
				if (activity != act && activity != null) {
					Log.d("finish", activity + "");
					activity.finish();
					activityList.set(i, null);
				}

			}

			for (int i = 0; i < activityList.size(); i++) {
				if (activityList.get(i) == null) {
					activityList.remove(i);
				}

			}
		}

	}

	public void exit() {
		closeAllActivities();
		System.exit(0);

	}
}
